from threading import Thread
from os import system, listdir
from webbrowser import open as open_url


class ViewData():
    def __init__(self):
        super(ViewData, self).__init__()

    def generateSeries(self, down, combo, change):
        """ Open dock for choise video player. """
        change.clear()
        name = "_".join(combo.currentText().split())
        list_path = listdir(f'{down}/{name}/')
        replace_list = tuple((i.split('-')[-1].split()[0] for i in list_path))
        change.addItems(replace_list)

    def player(self, series, player, combo, down):
        """ Open anime in video player. """
        player = 'xdg-open' if player in 'default' else 'cvlc' \
                            if player in 'vlc' else player
        name = '_'.join(combo.currentText().split())
        link = f'{down}/{name}/{name}-{series} серия.mp4'
        Thread(target=system, args=(f'{player} "{link}"',)).start()

    def open(self, data, mode, seticon, but, index, changeIcon, track, ind):
        """ Open URL in browser for view info. """
        but.setIcon(seticon('domain'))
        Thread(target=changeIcon).start()
        link = (data['anime']['track-link'], *data['manga']['track-link'])
        rez, note = data[mode]['urls'], f'notify-send "List <{mode}> is empty"'
        url = link[ind][index] if track and ind else link[ind] if track else \
              rez[index]
        condition = link[ind] if track and ind else rez

        open_url(url) if condition else system(note)

    def loger(self, data, tab, mode):
        """ Show log file. """
        log = 'logs' if tab == 1 else 'log'
        return data[mode][log] if data[mode][log] else 'No file exist...'

    def showed(self, data, index, combo, seticon):
        """ Show items in comboboxes. """
        child = ('anime', 'manga', 'ranobe')[index]
        name = 'names' if index in (1, 2) else 'name'
        icon = seticon(('animevost', 'mask', 'ranobe')[index])

        combo.clear()
        [combo.addItem(icon, v) for i, v in enumerate(data[child][name])]

    def about(self, data, widget):
        """ Getting small info about working of application. """
        widget.setHtml(data['about'])
